<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$tmpl = array (
                	'table_open' => '<table class="bordered-table zebra-striped">',
              	);

		$this->table->set_template($tmpl);

		$data['feelings'] = $this->api_model->getFeelings();
		$this->load->view('admin', $data);
	}

	public function empty_db()
	{
		dlog('bdd vidée');
		$this->api_model->emptyDB('feelings');
		echo 'bdd vidée';
	}

	public function new_rand_values($nb)
	{
		$virgule = 6;
		$centreLat = 45379282;
		$centreLong = -71928799;
		$delta = 30000;

		$feelings = array('SERENITY', 'JOY', 'ECSTACY', 'TRUST', 'ACCEPTANCE', 'ADMIRATION', 'APPREHESION', 'FEAR', 'TERROR', 'DISTRACTION', 'SURPRISE','AMAZEMENT','PENSIVENESS','SADNESS','GRIEF','BOREDOM','DISGUST','LOATHING','ANNOYANCE','ANGER','RAGE', 'INTEREST', 'ANTICIPATION', 'VIGILANCE');

		for ($i = 0; $i < $nb; $i++) 
		{
			$data[$i] = array(
					'feeling_name' => $feelings[mt_rand(0, count($feelings) - 1)],
					'latitude' => $this->_floatAleatoire($centreLat, $delta, $virgule),
					'longitude' => $this->_floatAleatoire($centreLong, $delta, $virgule),
					'date' => date('Y-m-d H:i:s')
				);

			$this->api_model->createFeeling($data[$i]);
		}

		$tmpl = array (
                	'table_open' => '<table class="bordered-table zebra-striped">',
              	);

              	$this->table->set_caption($nb . ' valeurs ajoutées');
		$this->table->set_heading('Feeling', 'Latitude', 'Longitude', 'Date');
		$this->table->set_template($tmpl);

		echo $this->table->generate($data);
	}

	public function get_feelings()
	{
		$data = $this->api_model->getFeelings();

		$tmpl = array (
                	'table_open' => '<table class="bordered-table zebra-striped">',
              	);

              	$this->table->set_heading('Id', 'Feeling', 'Latitude', 'Longitude', 'Date');
		$this->table->set_template($tmpl);

		dlog($data);

		if($data->num_rows() != 0)
			echo $this->table->generate($data);
		else
			echo "<em>Il n'y pas de feelings</em>";
	}

	// Tools
	private function _floatAleatoire($val, $delta, $virgule) 
	{
		$val = mt_rand($val-$delta, $val+$delta); // valeur en int
		$retour = $val / pow(10.0, $virgule);
		return $retour;
	}


	// Au cas ou
	private function _test()
	{
		$url = 'http://ift604.alixg.com/api/feeling/';
		$data = array(
				'feeling_name' => 'Sadness',
				'latitude' => '45.3787',
				'longitude' => '-71.9295'
			);

		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

		$response = curl_exec($ch);
		if(!$response) {
			return false;
		}

		print_r($response);
	}


}

/* End of file admin.php */
/* Location: ./system/application/controllers/admin.php */